package appBCN.version;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import appBCN.MOs.Person;

public class SeeDates extends Activity {
	private Person user;
	private PopupWindow pw;
	private final SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MMM-yyyy");
	private final SimpleDateFormat dateFormatter2 = new SimpleDateFormat("dd-MM-yyyy");
	
	@Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		setContentView(R.layout.see_dates);
    	user = AppBCNApplication.getInstance().getPerson();
    	if(user.getDates() != null && user.getDates().size() > 0)
    	{
	    	Iterator it = user.getDates().iterator();
	    	List<String> listDates = new ArrayList<String>();
	    	while(it.hasNext())
	    	{
	    		Date t = (Date)it.next();
	    		listDates.add("Cita "+it.hashCode()+"\nFecha:"+dateFormatter2.format(t)+"\nHora: "+t.getHours()+":"+t.getMinutes());
	    	}
	        ArrayAdapter ad=new ArrayAdapter(this, R.layout.list_item, listDates);
	        final ListView listView=(ListView)findViewById(R.id.listViewSeeDates);
	        listView.setAdapter(ad);
			listView.setOnItemClickListener(new OnItemClickListener()
			{
				public void onItemClick(AdapterView arg0, View arg1, int arg2,long arg3) 
				{
					Date date = new Date();
					try {
						date = dateFormatter.parse(arg0.getItemAtPosition(arg2).toString());
					} catch (ParseException e) {
						e.printStackTrace();
					}
					final Date dateUser;
					if(date != null)
					{
						dateUser = date;
					}
					else
					{
						dateUser = new Date();
					}
					String textMessage = "Ha escollit borrar "+arg0.getItemAtPosition(arg2)+"\nConfirmar?";
					AlertDialog.Builder builder = new AlertDialog.Builder(listView.getContext());
					builder.setMessage(textMessage)
			       .setCancelable(false)
			       .setPositiveButton("Si", new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {
			        	   user.getDates().remove(dateUser);
			        	   AppBCNApplication.getInstance().setPerson(user);
			        	   SeeDates.this.finish();
			           }
			       })
			       .setNegativeButton("No", new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {
			                dialog.cancel();
			           }
			       });
					AlertDialog alert = builder.create();
					alert.show();		
				}
			 });
    	}
    	else
    	{
    		List def = new ArrayList<String>();
    		def.add("No hi ha cites");
    		ArrayAdapter ad=new ArrayAdapter(this, R.layout.list_item, def);
	        final ListView listView=(ListView)findViewById(R.id.listViewSeeDates);
	        listView.setAdapter(ad);
    	}
    }
}
